<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Let 命令</title>
</head>

<body>
    <p>1.let命令声明的变量只在let所在的代码块里有效</p>
    <div>
        <p>b:1</p>
        <p>Uncaught ReferenceError: a is not defined</p>
    </div>
    <hr />
    <p>2. for 循环体内 每次循环的迭代变量都是独立的，而且循环体外不可访问</p>
    <p>
        <pre> 
      for (let i = 0 ; i < 10 ; i++) {
       // ...  每次循环 i 都是独立的
      }
      console.log(i)； //ReferenceError: i is not defined 
  </pre>
    </p>
    <hr>
    <p>3. let 没有变量提升，必须先声明再使用</p>
    <div>
        <pre>
     console.log(bar); // ReferenceError
      let bar =2;
   </pre>
    </div>
    <hr>
    <p>
        4. let有暂时性死区，全局变量无法覆盖代码块内let声明的同名变量
    </p>
    <div>
        全局变量和代码块内的变量重名
        <pre>
    var tmp = 123;
    if(true) {
    tmp = 'abc'; // ReferenceError
    let tmp ;
    }
   </pre>
    </div>
    <hr>
    <p>5. 同一个作用域内，let不能重复声明一个变量,以下都报错</p>
    <div>
        <pre>
          function(){
            let a=10;
            var a=1; 
          }
        </pre>
        <pre>
          function(){
            let a=10;
            let  a=1; 
          }
        </pre>
    </div>
    <hr>
    <p> 6. 块级作用域，可以在块级作用域声明函数</p>
    <div>
        <pre>
          function f() {
            console.log('I am outside !');
          }
          (function() {
            if (false) {
              function f() {
                console.log('I am inside !');
              }
            }
            f();
          } ());
        </pre>
    </div>
</body>

</html>
<script>
    {
        var b = 1;
        let a = 10;
    }
    console.log("b:" + b);
    console.log("a:" + a);
    console.log('-----------------------')
    for (let i = 0; i < 10; i++) {
        // ...  每次循环 i 都是独立的
        console.log('循环体')
    }
    console.log(i); //ReferenceError: i is not defined 
    console.log('-----------------------')
    console.log('bar:' + bar); // ReferenceError
    let bar = 2;
    console.log('-----------------------')
    var tmp = 123;
    if (true) {
        tmp = 'abc'; // ReferenceError
        let tmp;
    }
    console.log('-----------------------')

    function a() {
        let a = 10;
        var a = 1;
    }

    function b() {
        let a = 10;
        let a = 1;
    }
    console.log('-----------------------')
</script>